package ac.manchester.cs.afzal.autoservd.file_handler;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;

/**
 * Title: AutoServD
 *
 * Description: A toolkit to find the relevance of candidate terms from a text corpus
 * to any particular topic/sub-domain using a term classification driven approach. The
 * system utilizes the lexical and contextual profiles of the candidate and domain-representing
 * "Resource Terms" (Seed and Ontological). The lexical and contextual profiles of candidate terms
 * are compared with those of Resource Terms to measure their relevance to the topic of interest.
 *
 * File_Manager class contains the methods to read/write the files, and download the files when URL
 * is given.
 * <p>Company: The University of Manchester</p>
 *
 * @author Hammad Afzal
 * @version 1.0
 */
public class File_Manager
{

    File readFile, writeFile;
    StringBuffer contents;

    /**
     * Reads the file from given path and put the contents into StringBuffer
     * @param input_path
     * @return
     */
    public StringBuffer fileReader(String input_path)
    {
        readFile = new File(input_path);
        contents = new StringBuffer();

        BufferedReader input = null;
        try
        {
            input = new BufferedReader(new FileReader(readFile));
            String line = null;
            while((line = input.readLine()) != null)
            {
                contents.append(line);
                contents.append(System.getProperty("line.separator"));
            }
        }
        catch (FileNotFoundException ex)
        {
            ex.printStackTrace();
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            try
            {
                if(input != null)
                {
                    input.close();
                }
            }
            catch (IOException ex)
            {
                ex.printStackTrace();
            }
        }
        return contents;
    }

    /**
     * Reads the given input file and put the contents into StringBuffer
     * @param inputFile
     * @return
     */
    public StringBuffer fileReader(File inputFile)
    {
        contents = new StringBuffer();

        BufferedReader input = null;
        try
        {
            input = new BufferedReader(new FileReader(inputFile));
            String line = null;
            while((line = input.readLine()) != null)
            {
                contents.append(line);
                contents.append(System.getProperty("line.separator"));
            }
        }
        catch (FileNotFoundException ex)
        {
            ex.printStackTrace();
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            try
            {
                if(input != null)
                {
                    input.close();
                }
            }
            catch (IOException ex)
            {
                ex.printStackTrace();
            }
        }
        return contents;
    }

    /**
     * Downloads the file from given string url_string and writes it into the output_path
     * @param url_string
     * @param output_path
     */
    public void fileDownloader(String url_string, String output_path)
    {
        try
        {
            URL url = new URL(url_string);
            InputStream urlStream = url.openStream();
            InputStreamReader iStr = new InputStreamReader(urlStream);
            BufferedReader reader = new BufferedReader(iStr);

            writeFile = new File(output_path);
            OutputStream fout = new FileOutputStream(writeFile);
            OutputStream bout = new BufferedOutputStream(fout);
            OutputStreamWriter writer = new OutputStreamWriter(bout,
                    "ISO-8859-1");
            String line = reader.readLine();

            while(line != null)
            {
                writer.write(line);
                writer.write(System.getProperty("line.separator"));
                line = reader.readLine();
            }
            urlStream.close();
            reader.close();
            writer.close();
        }
        catch (IOException e)
        {
        }
    }

    /**
     * Downloads the file from given URL and writes it into the output_path
     * @param url
     * @param output_path
     */
    public void fileDownloader(URL url, String output_path)
    {
        try
        {
            InputStream urlStream = url.openStream();
            InputStreamReader iStr = new InputStreamReader(urlStream);
            BufferedReader reader = new BufferedReader(iStr);

            writeFile = new File(output_path);
            OutputStream fout = new FileOutputStream(writeFile);
            OutputStream bout = new BufferedOutputStream(fout);
            OutputStreamWriter writer = new OutputStreamWriter(bout,
                    "ISO-8859-1");
            String line = reader.readLine();

            while(line != null)
            {
                writer.write(line);
                writer.write(System.getProperty("line.separator"));
                line = reader.readLine();
            }
            urlStream.close();
            reader.close();
            writer.close();
        }
        catch (IOException e)
        {
        }
    }

    /**
     * Writes the given StringBuffer in a file whose path is given as input
     * @param stringBuffer
     * @param output_path
     */
    public void fileWriter(StringBuffer stringBuffer, String output_path)
    {
        try
        {
            writeFile = new File(output_path);
            OutputStream fout = new FileOutputStream(writeFile);
            OutputStream bout = new BufferedOutputStream(fout);
            OutputStreamWriter writer = new OutputStreamWriter(bout);
            writer.write(stringBuffer.toString());
            writer.close();
            bout.close();
            fout.close();
        }
        catch (Exception ex)
        {
            System.out.println("Value of Exception is :" + ex.getMessage());
        }
    }

    /**
     * Writes the given String in a file whose path is given as input
     * @param str
     * @param path
     */
    public void fileWriter(String str, String path)
    {
        try
        {
            writeFile = new File(path);
            OutputStream fout = new FileOutputStream(writeFile);
            OutputStreamWriter writer = new OutputStreamWriter(fout, "ISO-8859-1");
            writer.write(str.toString());
            writer.close();
            fout.close();
        }
        catch (Exception ex)
        {
            System.out.println("Value of Exception is :" + ex.getMessage());
        }
    }

    /**
     * Returns the file name without extension
     * @param inputFileString
     * @param extension
     * @return
     */
    public String strip_file_name_from_file_name_plus_extension(String inputFileString, String extension)
    {
        int extension_length = extension.length() + 1;
        int inputFileLength = inputFileString.length();
        inputFileString = inputFileString.substring(0, inputFileLength - extension_length);
        return inputFileString;

    }

    /**
     * Returns the file name without extension
     * @param inputFileString
     * @param extension
     * @param delimiter
     * @return
     */
    public String strip_second_part_of_file_name_from_file_name_plus_extension_with_delimiter(String inputFileString, String extension, String delimiter)
    {
        int delimiter_index = inputFileString.indexOf(delimiter);
        int delimiter_length = delimiter.length();
        int inputFileLength = inputFileString.length();
        int extensionLength = extension.length() + 1;
        inputFileString = inputFileString.substring(delimiter_index + delimiter_length, inputFileLength - extensionLength);
        return inputFileString;

    }

    /**
     * Returns the file name without extension
     * @param inputFileString
     * @param extension
     * @param delimiter
     * @return
     */
    public String strip_first_part_of_file_name_from_file_name_plus_extension_with_delimiter(String inputFileString, String delimiter)
    {
        int delimiter_index = inputFileString.indexOf(delimiter);
        inputFileString = inputFileString.substring(0,delimiter_index);
        return inputFileString;

    }

    /**
     * Returns the extension of the input file
     * @param inputFile
     * @return
     */
    public String strip_extension_from_file_name_plus_extension(String inputFile)
    {
        int index_extension = inputFile.indexOf(".");
        int inputFileLength = inputFile.length();
        String extension = inputFile.substring(index_extension + 1, inputFileLength);
        return extension;

    }
}
